import {ChangeDetectionStrategy, ChangeDetectorRef, Component, OnInit, TemplateRef, ViewChild} from '@angular/core';
import {GoBackParam, PageTypeEnum, SearchCommonVO} from '../../../core/services/types';
import {MyTableConfig} from '../../../share/components/ant-table/ant-table.component';
import {PageHeaderType} from '../../../share/components/page-header/page-header.component';
import {ExamsManageListModel, ExamsManageService} from '../../../core/services/http/exams-manage/exams-manage.service';
import {NzModalService} from 'ng-zorro-antd/modal';
import {NzTableQueryParams} from 'ng-zorro-antd/table';
import {finalize} from 'rxjs/operators';
import {MapPipe} from '../../../share/pipes/map.pipe';
import {ActionCode} from 'src/app/configs/actionCode';

interface SearchParam {
  name: string;
  startTime: number;
  endTime: number;
}

@Component({
  selector: 'app-online-exam',
  templateUrl: './online-exam.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class OnlineExamComponent implements OnInit {
  @ViewChild('operationTpl', {static: true}) operationTpl!: TemplateRef<any>;
  ActionCode = ActionCode;
  searchParam: Partial<SearchParam> = {};
  currentPage: number;
  pageTypeEnum = PageTypeEnum;
  tableConfig!: MyTableConfig;
  pageHeaderInfo: Partial<PageHeaderType> = {
    title: '考试管理',
    breadcrumb: ['考试教育', '考试管理']
  };
  dataList: ExamsManageListModel[] = [];
  itemId!: number;

  constructor(private dataService: ExamsManageService, private modalSrv: NzModalService, private cdr: ChangeDetectorRef) {
    this.currentPage = this.pageTypeEnum.List;
  }


  /*返回功能*/

  // tslint:disable-next-line:typedef
  async returnToList(e?: GoBackParam) {
    this.currentPage = this.pageTypeEnum.List;
    if (!!e && e.refesh) {
      await this.getDataList({pageIndex: e.pageNo} as NzTableQueryParams);
    }
  }


  getDataList(e?: NzTableQueryParams): void {
    this.tableConfig.loading = true;
    const params: SearchCommonVO<any> = {
      pageSize: this.tableConfig.pageSize!,
      pageNum: e?.pageIndex || this.tableConfig.pageIndex!,
      filters: this.searchParam
    };
    this.dataService.getExamsManageList(params).pipe(finalize(() => {
      this.tableLoading(false);
    })).subscribe((data => {
      const {list, total, pageNum} = data;
      this.dataList = [...list];
      this.dataList.forEach(item => {
        if (item.startTime == null || item.endTime == null) {
          item.testTime = '不限时';
        } else {
          item.startTime = new MapPipe().transform(item.startTime, 'date:yyyy-MM-dd HH:mm');
          item.endTime = new MapPipe().transform(item.endTime, 'date:yyyy-MM-dd HH:mm');
          item.testTime = item.startTime + '~' + item.endTime;
        }
      });
      this.tableConfig.total = total!;
      this.tableConfig.pageIndex = pageNum!;
      this.tableLoading(false);
    }));
  }

  /*重置*/
  resetForm(): void {
    this.searchParam = {};
    this.getDataList();
  }

  // 触发表格变更检测
  tableChangeDectction(): void {
    // 改变引用触发变更检测。
    this.dataList = [...this.dataList];
    this.cdr.detectChanges();
  }

  tableLoading(isLoading: boolean): void {
    this.tableConfig.loading = isLoading;
    this.tableChangeDectction();
  }


  goTest(id: number): void {
    this.itemId = id;
    this.currentPage = this.pageTypeEnum.DetailOrExamine;
  }

  // 修改一页几条

  changePageSize(e: number): void {
    this.tableConfig.pageSize = e;
  }

  ngOnInit(): void {
    this.initTable();
  }

  private initTable(): void {
    this.tableConfig = {
      showCheckbox: false,
      headers: [
        {
          title: '考试名称',
          field: 'name',
          width: 100,
        },
        {
          title: '考试时间',
          width: 100,
          field: 'testTime',
        },
        {
          title: '考试时长',
          width: 50,
          field: 'duration',
        },
        {
          title: '考试总分',
          width: 50,
          field: 'countScore',
        },
        {
          title: '及格线',
          width: 50,
          field: 'passScore',
        },
        {
          title: '操作',
          tdTemplate: this.operationTpl,
          width: 70,
          fixed: true
        }
      ],
      total: 0,
      loading: true,
      pageSize: 10,
      pageIndex: 1,
    };
  }
}

